﻿CREATE PROCEDURE [dbo].[ETL_SP_InsertDtTableDataHistory]
	@Id uniqueidentifier,
	@DtTableId uniqueidentifier,
	@DataId nvarchar(512),
	@ColumnId uniqueidentifier,
	@NewValue nvarchar(512),
	@CreatedTime datetime,
	@Creator nvarchar(64),
	@Reason int,
	@OldValue nvarchar(512)
AS
	INSERT INTO [ETL_DtTableDataHistory] (Id, DtTableId, DataId, ColumnId, NewValue, CreatedTime, Creator, Reason, IsLatest, OldValue)
	VALUES (@Id, @DtTableId, @DataId, @ColumnId, @NewValue, @CreatedTime, @Creator, @Reason, 1, @OldValue);

	UPDATE [ETL_DtTableDataHistory] set [IsLatest] = 0 where [DtTableId]=@DtTableId and [DataId]=@DataId and [ColumnId]=@ColumnId and [Id] <> @Id;
RETURN 0
